package logging;

/**
 * @author Zhongzhicong
 * @date 2023/3/22 16:29
 * @version：1.0
 */
public class LogcLogger implements Logger{

    private String name;
    private Appender appender ;
    private Level level = Level.TRACE;
    private int effectiveLevelInt = level.toInt();

    private LogcLogger parent;
    private LoggerContext loggerContext;


    private void filterAndLog(Level level, String msg) {
        LoggingEvent e = new LoggingEvent(level, msg, getName());
        for (LogcLogger l = this; l != null; l = l.parent) {
            if (l.appender == null) {
                l.appender = new ConsoleAppender();
//                continue;
            }
            if (level.toInt() >= effectiveLevelInt) {
                appender.append(e);
            }
            break;
        }
    }

    @Override
    public void trace(String msg) {
        filterAndLog(Level.TRACE, msg);
    }

    @Override
    public void debug(String msg) {
        filterAndLog(Level.DEBUG, msg);
    }

    @Override
    public void info(String msg) {
        filterAndLog(Level.INFO, msg);
    }

    @Override
    public void warn(String msg) {
        filterAndLog(Level.WARN, msg);
    }

    @Override
    public void error(String msg) {
        filterAndLog(Level.ERROR, msg);
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Appender getAppender() {
        return appender;
    }

    public void setAppender(Appender appender) {
        this.appender = appender;
    }

    public Level getLevel() {
        return level;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    public int getEffectiveLevelInt() {
        return effectiveLevelInt;
    }

    public void setEffectiveLevelInt(int effectiveLevelInt) {
        this.effectiveLevelInt = effectiveLevelInt;
    }

    public LogcLogger getParent() {
        return parent;
    }

    public void setParent(Logger parent) {
        this.parent = (LogcLogger)parent;
    }

    public LoggerContext getLoggerContext() {
        return loggerContext;
    }

    public void setLoggerContext(LoggerContext loggerContext) {
        this.loggerContext = loggerContext;
    }
}
